
#include "stdafx.h"
#include <iostream>
#include "Atomic.h"
#include "FreeListPool.h"
#include "RelatedFun.h"
#include "ObjectPool.h"


class A{
public:
	A(){}
	virtual ~A(){
	}

};

class B : public A{
public:
};

class C{

};

void TestFreeListPool()
{

	const int nLen = 1024 * 1024;
	const int nSize = 7;
	char** test = new char*[nLen];

	unsigned uBegTime = WTB::GetClock();

	for (int nIndex = 0; nIndex < nLen; ++nIndex)
	{
		test[nIndex] = WTB::CFreeListPool::Instance()->GetMemory(nSize);
		//test[nIndex] = (byte*)malloc(nSize);
	}

	for (int nIndex = 0; nIndex < nLen; ++nIndex)
	{
		WTB::CFreeListPool::Instance()->ReleaseMemory(test[nIndex],nSize);
		//free(test[nIndex]);
	}

	unsigned uEndTime = WTB::GetClock();

	std::cout<<"take time: "<<uEndTime - uBegTime<<std::endl;

	delete []test;

}

void TestCPlusPlusObjectPool()
{

	const int nLen = 1024 * 1024;

	A** test = new A*[nLen];

	unsigned uBegTime = WTB::GetClock();

	for (int nIndex = 0; nIndex < nLen; ++nIndex)
	{
		test[nIndex] = WTB::CPlusPlusObjectPool<B>::Instance()->GetObject();
		//test[nIndex] = new B();
	}

	for (int nIndex = 0; nIndex < nLen; ++nIndex)
	{
		WTB::CPlusPlusObjectPool<B>::Instance()->ReleaseObject(dynamic_cast<B*>(test[nIndex]));
		//delete test[nIndex];
		//test[nIndex] = NULL;
	}

	unsigned uEndTime = WTB::GetClock();

	std::cout<<"take time: "<<uEndTime - uBegTime<<std::endl;

	delete []test;
}

void TestObjectPool()
{

	const int nLen = 1024 * 1024;

	C** test = new C*[nLen];

	unsigned uBegTime = WTB::GetClock();

	for (int nIndex = 0; nIndex < nLen; ++nIndex)
	{
		test[nIndex] = WTB::CObjectPool<C>::Instance()->GetObject();
		//test[nIndex] = new B();
	}

	for (int nIndex = 0; nIndex < nLen; ++nIndex)
	{
		WTB::CObjectPool<C>::Instance()->ReleaseObject(test[nIndex]);
		//delete test[nIndex];
		//test[nIndex] = NULL;
	}

	unsigned uEndTime = WTB::GetClock();

	std::cout<<"take time: "<<uEndTime - uBegTime<<std::endl;

	delete []test;
}

/*
void main(){

	TestFreeListPool();
	TestCPlusPlusObjectPool();
	TestObjectPool();
};
*/